package com.csii.adhoc.util.encrypt;

/**
 * 16进制转换工具类
 */
public class HexUtils {
    private static final char[] hexChar = "0123456789abcdef".toCharArray();

    /**
     * byte装16进制
     * @param bcd 入参
     * @return 转化后结果
     */
    public static String bytes2HexStr(byte[] bcd) {
        StringBuilder s = new StringBuilder(bcd.length * 2);
        for (byte b : bcd) {
            s.append(hexChar[b >>> 4 & 15]);
            s.append(hexChar[b & 15]);
        }
        return s.toString();
    }

    /**
     * 16进制转回byte数组
     * @param s  入参
     * @return  返回
     */
    public static byte[] hexStr2Bytes(String s) {
        byte[] bytes = new byte[s.length() / 2];
        for (int i = 0; i < bytes.length; ++i) {
            bytes[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
        }
        return bytes;
    }
}
